package com.zhipu.balance.mobile.dao;

import android.text.TextUtils;

import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.zhipu.balance.mobile.event.RecordRapeseed;
import com.zhipu.balance.mobile.event.RecordRice;
import com.zhipu.balance.mobile.event.RecordRice_Table;
import com.zhipu.balance.mobile.event.RecordSoybean;
import com.zhipu.balance.mobile.event.RecordSoybean_Table;
import com.zhipu.balance.mobile.event.RecordSoybean;
import com.zhipu.balance.mobile.event.RecordSoybean_Table;

import java.util.List;

public class RecordSoybeanDao {

    private RecordSoybeanDao() {
    }

    private static final class SingletonHolder {
        private static final RecordSoybeanDao INSTANCE = new RecordSoybeanDao();
    }

    public static RecordSoybeanDao getInstance() {
        return SingletonHolder.INSTANCE;
    }





    /**
     * 获取所有数据
     *
     * @return
     */
    public List<RecordSoybean> getAllList() {
        return SQLite.select().from(RecordSoybean.class).where().orderBy(RecordSoybean_Table.recordSoybeanId, true).queryList();
    }


    /**
     * 分页获取
     *
     * @param nowPage
     * @param uniqueId
     * @return
     */
    public List<RecordSoybean> PagingFetch(int nowPage, String uniqueId) {
        if (TextUtils.isEmpty(uniqueId)) {
            return SQLite.select().from(RecordSoybean.class).where().orderBy(RecordSoybean_Table.recordSoybeanId, false).limit(9).offset(nowPage).queryList();
        } else {
            return SQLite.select().from(RecordSoybean.class).where(RecordSoybean_Table.uniqueId.like("%" + uniqueId + "%")).orderBy(RecordSoybean_Table.recordSoybeanId, false).limit(9).offset(nowPage).queryList();
        }
    }

    /**
     * 根据编号查询
     * @param productCategoryText
     * @return
     */
    public RecordSoybean QueryByNumber(String productCategoryText){
        RecordSoybean mRecordWheat = SQLite.select().from(RecordSoybean.class).where(RecordSoybean_Table.uniqueId.eq(productCategoryText)).querySingle();
        if(null == mRecordWheat){
            mRecordWheat = new RecordSoybean();
        }
        return mRecordWheat;
    }


    /**
     * 总条数
     * @return
     */
    public long TotalCount(String uniqueId){
        if(TextUtils.isEmpty(uniqueId)){
            return SQLite.selectCountOf().from(RecordSoybean.class).count();
        }else {
            return SQLite.selectCountOf().from(RecordSoybean.class).where(RecordSoybean_Table.uniqueId.eq(uniqueId)).count();
        }
    }


    /**
     * 根据编号获取数据
     * @param uniqueId
     * @return
     */
    public List<RecordSoybean> getUniqueIdList(String uniqueId){
        List<RecordSoybean> mRecordSoybeanList = null;
        if (!TextUtils.isEmpty(uniqueId)) {
            mRecordSoybeanList = SQLite.select().from(RecordSoybean.class).where(
                    RecordSoybean_Table.uniqueId.like("%" + uniqueId + "%"),
                    RecordSoybean_Table.impurityAverageValue.eq(false),
                    RecordSoybean_Table.damagedGrainValue.eq(false),
                    RecordSoybean_Table.thermalDamageValue.eq(false),
                    RecordSoybean_Table.immatureBrokenGrainValue.eq(false)
            ).queryList();
        }
        return mRecordSoybeanList;
    }
}
